package cn.iocoder.yudao.module.fengdong.smartelderly.service.complaint;

import jodd.util.StringUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Pattern;

import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.complaint.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.complaint.ComplaintDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fengdong.smartelderly.convert.complaint.ComplaintConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.mysql.complaint.ComplaintMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.fengdong.smartelderly.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 投诉管理 Service 实现类
 *
 * @author 沣东养老院管理员
 */
@Service
@Validated
public class ComplaintServiceImpl implements ComplaintService {

    @Resource
    private ComplaintMapper complaintMapper;

    @Override
    public Long createComplaint(ComplaintCreateReqVO createReqVO) {
        if (StringUtil.isNotBlank(createReqVO.getContactInformation())) {
            isValidContactInformation(createReqVO.getContactInformation());
        }
        // 插入
        ComplaintDO complaint = ComplaintConvert.INSTANCE.convert(createReqVO);
        complaintMapper.insert(complaint);
        // 返回
        return complaint.getId();
    }

    /**
     * 联系方式校验  只允许手机号和邮箱
     * @param contactInformation
     */
    public static void isValidContactInformation(String contactInformation) {
        // 手机号正则表达式
        String phoneRegex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
        // 邮箱正则表达式
        String emailRegex = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        boolean ruslt = Pattern.matches(phoneRegex, contactInformation) || Pattern.matches(emailRegex, contactInformation);
        if (!ruslt) {
            throw exception(VALID_CONTACT_INFORMATION);
        }
    }
    @Override
    public void updateComplaint(ComplaintUpdateReqVO updateReqVO) {
        if (updateReqVO.getContactInformation() != null || !"".equals(updateReqVO.getContactInformation())) {
            isValidContactInformation(updateReqVO.getContactInformation());
        }
        // 校验存在
        validateComplaintExists(updateReqVO.getId());
        // 更新
        ComplaintDO updateObj = ComplaintConvert.INSTANCE.convert(updateReqVO);
        complaintMapper.updateById(updateObj);
    }

    @Override
    public void deleteComplaint(Long id) {
        // 校验存在
        validateComplaintExists(id);
        // 删除
        complaintMapper.deleteById(id);
    }

    private void validateComplaintExists(Long id) {
        if (complaintMapper.selectById(id) == null) {
            throw exception(COMPLAINT_NOT_EXISTS);
        }
    }

    @Override
    public ComplaintDO getComplaint(Long id) {
        return complaintMapper.selectById(id);
    }

    @Override
    public List<ComplaintDO> getComplaintList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return complaintMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ComplaintDO> getComplaintPage(ComplaintPageReqVO pageReqVO) {
        return complaintMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ComplaintDO> getComplaintList(ComplaintExportReqVO exportReqVO) {
        return complaintMapper.selectList(exportReqVO);
    }

    @Override
    public void updatedispose(ComplaintUpdatedisposeReqVO updateReqVO) {
        // 校验存在
        validateComplaintExists(updateReqVO.getId());
        // 更新
        ComplaintDO updateObj = ComplaintConvert.INSTANCE.convertDispose(updateReqVO);
        updateObj.setDisposeTime(LocalDateTime.now());
        complaintMapper.updateById(updateObj);
    }

}
